package com.ruanda.spongebobrunner.game;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import lombok.Data;

/**
 * 玩家角色封装类
 */
@Data
public class Player {
    private Rect bounds;
    private int groundLevel;
    private int jumpHeight;
    private int jumpSpeed;
    private int fallSpeed;
    private boolean isJumping;
    private boolean isFalling;
    private int jumpCounter;
    private int fallCounter;
    private Bitmap[] animationFrames;
    private int currentFrame;
    private int frameCounter;
    private Matrix scaleMatrix;
    private Paint paint;

    public Player() {
        bounds = new Rect();
        scaleMatrix = new Matrix();
        paint = new Paint();
        paint.setAntiAlias(true);
    }

    /**
     * 初始化玩家
     * 
     * @param x           起始x坐标
     * @param y           起始y坐标
     * @param width       宽度
     * @param height      高度
     * @param groundLevel 地面高度
     */
    public void init(int x, int y, int width, int height, int groundLevel) {
        bounds.set(x, y, x + width, y + height);
        this.groundLevel = groundLevel;
        this.jumpHeight = height;
        this.jumpSpeed = 25;
        this.fallSpeed = 20;
        this.isJumping = false;
        this.isFalling = false;
        this.jumpCounter = 0;
        this.fallCounter = 0;
        this.currentFrame = 0;
        this.frameCounter = 0;
    }

    /**
     * 设置动画帧
     * 
     * @param frames 动画帧数组
     */
    public void setAnimationFrames(Bitmap[] frames) {
        this.animationFrames = frames;
    }

    /**
     * 更新玩家状态
     */
    public void update() {
        // 更新跳跃状态
        if (isJumping) {
            jumpCounter += jumpSpeed;
            bounds.offsetTo(bounds.left, bounds.top - jumpSpeed);

            // 达到跳跃高度后开始下落
            if (jumpCounter >= jumpHeight) {
                isJumping = false;
                isFalling = true;
                jumpCounter = 0;
            }
        }

        // 更新下落状态
        if (isFalling) {
            fallCounter += fallSpeed;
            bounds.offsetTo(bounds.left, bounds.top + fallSpeed);

            // 落到地面后停止下落
            if (bounds.bottom >= groundLevel) {
                bounds.offsetTo(bounds.left, groundLevel - bounds.height());
                isFalling = false;
                fallCounter = 0;
            }
        }

        // 更新动画帧
        frameCounter++;
        if (frameCounter >= 5) { // 每5帧切换一次动画
            currentFrame = (currentFrame + 1) % (animationFrames != null ? animationFrames.length : 1);
            frameCounter = 0;
        }
    }

    /**
     * 执行跳跃
     */
    public void jump() {
        if (!isJumping && !isFalling) {
            isJumping = true;
        }
    }

    /**
     * 绘制玩家
     * 
     * @param canvas     画布
     * @param spongeAnim 动画帧数组
     * @param isDying    是否正在死亡
     * @param velocityY  Y轴速度
     * @param animIndex  动画索引
     */
    public void draw(Canvas canvas, Bitmap[] spongeAnim, boolean isDying, int velocityY, int animIndex) {
        if (spongeAnim != null && spongeAnim.length > 0) {
            // 根据玩家状态选择动画帧
            int frameIndex = 0;
            if (isDying) {
                // 使用独立的死亡动画帧（最后一帧）
                frameIndex = spongeAnim.length - 1;
            } else if (velocityY < 0) {
                // 跳跃时使用特定帧
                frameIndex = 1;
            } else if (velocityY > 0) {
                // 下落时使用特定帧
                frameIndex = 2;
            } else {
                // 地面时使用行走动画
                frameIndex = animIndex % spongeAnim.length; // 使用游戏内计时保持动画稳定
            }

            // 检查动画帧是否为null
            if (spongeAnim[frameIndex] != null) {
                // 获取玩家的宽度和高度
                int playerWidth = bounds.width();
                int playerHeight = bounds.height();

                // 使用预缩放位图保持比例一致
                Bitmap scaledBitmap = Bitmap.createScaledBitmap(
                        spongeAnim[frameIndex],
                        playerWidth,
                        playerHeight,
                        true);

                // 设置画笔
                Paint paint = new Paint();
                paint.setAntiAlias(true);

                // 绘制玩家
                canvas.drawBitmap(scaledBitmap, bounds.left, bounds.top, paint);
            } else {
                Paint paint = new Paint();
                paint.setColor(android.graphics.Color.YELLOW);
                canvas.drawRect(bounds, paint);
            }
        } else {
            Paint paint = new Paint();
            paint.setColor(android.graphics.Color.YELLOW);
            canvas.drawRect(bounds, paint);
        }
    }

    /**
     * 检查是否与障碍物碰撞
     * 
     * @param obstacle 障碍物矩形
     * @return 是否碰撞
     */
    public boolean checkCollision(Rect obstacle) {
        return Rect.intersects(bounds, obstacle);
    }

    /**
     * 获取玩家边界
     * 
     * @return 玩家边界
     */
    public Rect getBounds() {
        return bounds;
    }

    /**
     * 设置玩家位置
     * 
     * @param x x坐标
     * @param y y坐标
     */
    public void setPosition(int x, int y) {
        bounds.offsetTo(x, y);
    }

    /**
     * 检查是否正在跳跃
     * 
     * @return 是否正在跳跃
     */
    public boolean isJumping() {
        return isJumping;
    }

    /**
     * 检查是否正在下落
     * 
     * @return 是否正在下落
     */
    public boolean isFalling() {
        return isFalling;
    }

    /**
     * 设置地面高度
     * 
     * @param groundLevel 地面高度
     */
    public void setGroundLevel(int groundLevel) {
        this.groundLevel = groundLevel;
    }
}